using UnityEngine;
using System.Collections;

public class AutomaticMove : MonoBehaviour
{
    #region Attributes

    public float distance = 0.0f;
    public float speed = 0.0f;
    public Vector3 direction = Vector3.zero;
    public bool debugMode = false;

    private bool isReturning = false;
    private Vector3 startPosition;
    private Vector3 endPosition;

    #endregion

    #region Methods

    private void precalculateVectors()
    {
        //this.direction.Normalize();
        this.endPosition = this.startPosition + this.direction * this.distance;
    }

    #endregion

    #region Unity Methods
    
    void Start () 
    {
        this.startPosition = this.transform.position;
        this.precalculateVectors();
	}
	
	void Update () 
    {
        if (this.debugMode)
            this.precalculateVectors();

        Vector3 displacement = this.direction * this.speed * Time.deltaTime;
        float distance = 0.0f;

        if (this.isReturning)
        {
            this.transform.Translate(displacement * -1);
            distance = Vector3.Distance(this.startPosition, this.transform.position);
        }
        else
        {
            this.transform.Translate(displacement);
            distance = Vector3.Distance(this.endPosition, this.transform.position);
        }

        if (distance < 1.0f)
        {
            this.isReturning = !this.isReturning;
        }
    }

    #endregion
}
